package com.swufe.lianxi;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MyLIstActivity extends AppCompatActivity implements AdapterView.OnItemClickListener,AdapterView.OnItemLongClickListener{
    private static final String TAG = "mylist";
    Handler handler;
    ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_list);
        //ArrayList<HashMap<String,String>>listItems=null;
        //准备测试数据
       ArrayList<HashMap<String,String>>listItems=new ArrayList<HashMap<String,String>>();
       //ArrayList<HashMap<String,String>>listItem=new ArrayList<HashMap<String,String>>();
      //  for (int i=0;i<30;i++){
        //    HashMap<String,String>map=new HashMap<String,String>();
          //  map.put("ItemTitle:","Rate:"+i);//标题
            //map.put("ItemDetail:","Detail:"+i);//内容
            //listItem.add(map);
        //生成适配器的Item和动态数组相应元素
        SimpleAdapter listItemAdapter=new SimpleAdapter(this, listItems,R.layout.list_item,
                new String[]{"ItemTitl","ItemDetail"},
                new int[]{R.id.itemTitle,R.id.itemDetail});

        //ListAdapter adapter=new ArrayAdapter<>(this,R.layout.list_item, listItems);
        MyAdapter adapter=new MyAdapter(this,android.R.layout.simple_list_item_1, listItems);
        listView=findViewById(R.id.mylist);
        listView.setAdapter(adapter);
        listView.setOnItemLongClickListener(this);

        handler = new Handler(Looper.myLooper()) {
            //收到数据
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (msg.what == 5) {
                    List<String> list = (List<String>) msg.obj;
                    ListAdapter adapter = new ArrayAdapter<String>(MyLIstActivity.this,
                            R.layout.list_item,
                            list);
                    //MyAdapter myAdapter = new MyAdapter(RatelistActivity2.this, R.layout.list_item, list);
                    //listView.setAdapter(listItemAdapter);
                    listView.setAdapter(adapter);
                }
                super.handleMessage(msg);
            }
        };
        MapTask mapTask=new MapTask();
        mapTask.setHandler(handler);
        Thread t=new Thread(mapTask);
        t.start();

        }



    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        TextView title=view.findViewById(R.id.itemTitle);
        TextView detail=view.findViewById(R.id.itemDetail);
        String detail2=String.valueOf(detail.getText());
        String title2=String.valueOf(title.getText());
        Log.i(TAG, "onItemClick: title2:"+title2);
        Log.i(TAG, "onItemClick: detail2:"+detail2);

        Intent Rate=new Intent(MyLIstActivity.this,RateOption.class);
        Rate.putExtra("Rate_Title",100f/Float.parseFloat(detail2));
        Rate.putExtra("Rate_value",title2);
        startActivityForResult(Rate,8);
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
        HashMap<String,String>map= (HashMap<String, String>) listView.getItemAtPosition(i);
        return true;//屏蔽单击事件
    }
}